package com.epro.dx.activity.filter;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.epro.dx.R;
import com.epro.dx.activity.BaseFragmentActivity;
import com.epro.dx.listenner.MyAsyncTaskListenner;
import com.epro.dx.task.GetTopCategoryAsyncTask;
import com.epro.dx.task.SaveOneKeyFilterStateAsyncTask;
import com.epro.dx.util.ActivityUtil;
import com.epro.dx.util.AsyncTaskUtil;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.model.CatelogModel;
import com.weipu.common.util.AlertToast;
import com.weipu.common.util.StringUtil;

/************************
 * @author Tony
 * @Date:2016-3-24下午1:59:26
 * @描述:过滤方式选择页面
 ************************/
public class FilterActivity extends BaseFragmentActivity implements OnClickListener {

	private RelativeLayout categoryContain;
	/**
	 * @描述：标题栏右侧完成按钮
	 */
	private TextView topbarApply;
	private Intent intentFrom;//上个页面传过来的数据
	private Bundle bundleFromIntent = null;
	private TextView categoryText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sendScreen();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_filter);
		//设置标题栏
		setTitleContent(R.string.page_title_filter);
		setLeftBtnContent(true, -1);
		initView();
		getDatasFromIntent();
		setDatas();
	}
	
	/**
	 * @创建：Tony 2016-3-31下午4:31:32
	 * @描述：获取从上个页面携带的数据
	 */
	private void getDatasFromIntent() {
		intentFrom = getIntent();
		if(null!=intentFrom){
			bundleFromIntent  = intentFrom.getBundleExtra(ServerConstant.OneKeyFilter.FILTERBUNDLE);
		}
	}

	/**
	 * @创建：Tony 2016-3-31下午4:28:42
	 * @描述：设置页面显示数据
	 */
	private void setDatas() {
		if(bundleFromIntent!=null){
			ArrayList<CatelogModel> catelogModels = bundleFromIntent.getParcelableArrayList(ServerConstant.OneKeyFilter.WHITECATEGORY);
			if(null!=catelogModels && catelogModels.size()>0){
				StringBuilder sb = new StringBuilder();
				for(int i = 0;i<catelogModels.size();i++){
					String name=catelogModels.get(i).getShortName();
					String text;
					if(i!=catelogModels.size()-1){
						text = name+",";
					}else{
						text = name;
					}
					sb.append(text);
				}
				String whiteCategorys = sb.toString();
				if(!TextUtils.isEmpty(whiteCategorys)){
					categoryText.setText(whiteCategorys);
				}
			}
		}
	}
	/**
	 * @创建：Tony 2016-3-24下午3:00:46
	 * @描述：初始化控件
	 */
	private void initView() {
		categoryContain = (RelativeLayout) findViewById(R.id.rl_category);
		categoryContain.setOnClickListener(this);
		topbarApply = (TextView) findViewById(R.id.filter_topbar_apply);
		topbarApply.setOnClickListener(this);
		
		categoryText = (TextView) findViewById(R.id.filter_category_texts);
		topbarLeftTv.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//点击进入品类选择页面
		case R.id.rl_category:
			sendGAEvent("点击按钮进入品类选择页面");
			Intent intent = new Intent(this,CategoryFilterActivity.class);
			if(null!=bundleFromIntent){
				intent.putExtra(ServerConstant.OneKeyFilter.FILTERBUNDLE, bundleFromIntent);
			}
			startActivityForResult(intent, 0);
			break;
		//点击保存并应用设置
		case R.id.filter_topbar_apply:
			//开启过滤模式，并跳转到主页
			sendGAEvent("点击Apply按钮");
			StringUtil.saveConfig("Home_RefreshBFD", "true");
			final String configText;
			if(ActivityUtil.isLogin(FilterActivity.this)){
				//调用接口设置一键过滤开启
				configText = ActivityUtil.getCustomerID()+ServerConstant.OneKeyFilter.ISFILTERKEY_ONLINE;
			}else{
				configText = ServerConstant.OneKeyFilter.ISFILTERKEY_LOCAL;
			}
			if("false".equals(StringUtil.getConfig(configText, "false"))){//没有开启过滤模式根据登录情况开启
				if(ActivityUtil.isLogin(FilterActivity.this)){//已经登录，调用接口开启过滤模式
					AsyncTaskUtil.executeTask(new SaveOneKeyFilterStateAsyncTask(FilterActivity.this,true, new MyAsyncTaskListenner() {
						
						@Override
						public void onPreTask() {
							showWaitingDialog();
						}
						
						@Override
						public void onPostTask(String result, Object obj) {
							hideWaitingDialog();
							if("0".equals(result)){
								StringUtil.saveConfig(configText, "true");
								//更新一级品类数据库
								AsyncTaskUtil.executeTask(new GetTopCategoryAsyncTask(FilterActivity.this, null));
								finish();
								overridePendingTransition(R.anim.alpha_in_300, R.anim.push_right_out);
							}else{
								AlertToast.alert(FilterActivity.this, ServerConstant.ReturnCode.MESSAGEMAP.get(result));
							}
						}
					}));
				}else{//没有登录，更改本地保存的状态
					StringUtil.saveConfig(configText, "true");
					StringUtil.saveConfig("Home_RefreshBFD", "true");
					//更新一级品类数据库
					AsyncTaskUtil.executeTask(new GetTopCategoryAsyncTask(FilterActivity.this, null));
					finish();
					overridePendingTransition(R.anim.alpha_in_300, R.anim.push_right_out);
				}
			}else{//已经开启过滤模式，不做操作直接返回首页
				finish();
				overridePendingTransition(R.anim.alpha_in_300, R.anim.push_right_out);
			}
			break;
		case R.id.topbar_left_tv:
			finish();
			overridePendingTransition(R.anim.alpha_in_300, R.anim.push_right_out);
			break;
			
		default:
			break;
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			finish();
			overridePendingTransition(R.anim.alpha_in_300, R.anim.push_right_out);
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent intent) {
		if(null!=intent){
			Bundle bundle = intent.getBundleExtra(ServerConstant.OneKeyFilter.FILTERBUNDLE);
			if(null!= bundle){
				bundleFromIntent = bundle;
				setDatas();
			}
		}
		super.onActivityResult(arg0, arg1, intent);
	}
}
